package me.linczh.bank.statistic;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import me.linczh.bank.bean.AccountBean;
import me.linczh.bank.bean.AccountStatisticBean;
import me.linczh.bank.dao.AccountDao;
import me.linczh.bank.dao.AccountStatisticDao;
import me.linczh.bank.dao.DaoFactory;
import me.linczh.bank.service.AccountService;
import me.linczh.framework.query.Query;

public class DailyTimerTask extends TimerTask {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("daily timer task");
		boolean doMonthTask = false;
		SimpleDateFormat fmt = new SimpleDateFormat("dd");
		if (fmt.format(new Date()).equals("01")) doMonthTask = true;
		/*
		Map<String, AccountStatisticBean> statisticMap = FileStoreHolder.getStatisticRecords();
		Map<String, AccountBean> accountMap = FileStoreHolder.getAccountRecords();
		
		for (String account : statisticMap.keySet()) {
			AccountStatisticBean statisticBean = statisticMap.get(account);
			AccountBean accountBean = accountMap.get(account);
			statisticBean.setBalance(statisticBean.getBalance() + accountBean.getBalance());
			statisticBean.setNumday(statisticBean.getNumday() + 1);

			// daily task
			if (!accountBean.getFrozen()) {
				if (accountBean.getBalance() < 0) {
					Calendar c = Calendar.getInstance();
					c.setTime(statisticBean.getOverdrawtime());
					c.add(Calendar.MONTH, 1);
					if (c.getTime().before(new Date())) {
						accountBean.setFrozen(true);
					}
				}
			} else {
				if (accountBean.getBalance() >= 0) accountBean.setFrozen(false);
				statisticBean.setOverdrawtime(null);
			}
			
			// month task
			if (doMonthTask) {
				if (statisticBean.getBalance() / statisticBean.getNumday() < 100000) {
					statisticBean.setChargemonth(statisticBean.getChargemonth() + 1);
				} else {
					statisticBean.setChargemonth(0);
				}
				if (statisticBean.getChargemonth() >= 2) {
					// charge
					AccountService service = new AccountService();
					if (service.canDrawal(accountBean, 1000)) {
						accountBean.setBalance(accountBean.getBalance() - 1000);
					} else {
					}
				}
				statisticBean.setBalance(0);
				statisticBean.setNumday(0);
			}
		}
		
		FileStoreHolder.flushAccountRecords();
		FileStoreHolder.flushStatisticRecords();
		*/
		AccountDao accountDao = DaoFactory.getAccountDao();
		AccountStatisticDao statisticDao = DaoFactory.getAccountStatisticDao();
		List<AccountStatisticBean> statisticList = statisticDao.query(new Query<AccountStatisticBean>() {
			@Override
			public boolean matched(AccountStatisticBean item) {
				// TODO Auto-generated method stub
				return true;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from statistic_tbl";
			}
		});
		for (AccountStatisticBean statisticBean : statisticList) {
			AccountBean accountBean = accountDao.findById(statisticBean.getAccount());
			statisticBean.setBalance(statisticBean.getBalance() + accountBean.getBalance());
			statisticBean.setNumday(statisticBean.getNumday() + 1);

			// daily task
			if (!accountBean.getFrozen()) {
				if (accountBean.getBalance() < 0) {
					Calendar c = Calendar.getInstance();
					c.setTime(statisticBean.getOverdrawtime());
					c.add(Calendar.MONTH, 1);
					if (c.getTime().before(new Date())) {
						accountBean.setFrozen(true);
					}
				}
			} else {
				if (accountBean.getBalance() >= 0) accountBean.setFrozen(false);
				statisticBean.setOverdrawtime(null);
			}
			
			// month task
			if (doMonthTask) {
				if (statisticBean.getBalance() / statisticBean.getNumday() < 100000) {
					statisticBean.setChargemonth(statisticBean.getChargemonth() + 1);
				} else {
					statisticBean.setChargemonth(0);
				}
				if (statisticBean.getChargemonth() >= 2) {
					// charge
					AccountService service = new AccountService();
					if (service.canDrawal(accountBean, 1000)) {
						accountBean.setBalance(accountBean.getBalance() - 1000);
					} else {
					}
				}
				statisticBean.setBalance(0);
				statisticBean.setNumday(0);
			}
			accountDao.update(accountBean);
			statisticDao.update(statisticBean);
		}
	}

}
